//
//  KPTool.swift
//  Sevengrass
//
//  Created by Kipp Dev on 2017/12/7.
//  Copyright © 2017年 Syrena. All rights reserved.
//

import Foundation
import Kingfisher
import BRPickerView
import Toast
import swiftScan
import Presentr


fileprivate let loadingHUD = JGProgressHUD.init(style: .dark).then {
    $0.vibrancyEnabled = true
    $0.shadow = JGProgressHUDShadow(color: .black, offset: .zero, radius: 5.0, opacity: 0.2)

}


// MARK: - 加载动画
public func showLoadingHUD(message: String? = "加载中...", detail: String? = "") {
    loadingHUD.textLabel.text = message
    loadingHUD.detailTextLabel.text = detail
    loadingHUD.show(in: windowRootViewController!.view)
}

// MARK: - 加载进度条
public func showProgressHUD(message: String = "加载中...") -> JGProgressHUD {
    let hud = JGProgressHUD.init(style: .dark)
    hud.vibrancyEnabled = true
    hud.indicatorView = JGProgressHUDRingIndicatorView()
    hud.shadow = JGProgressHUDShadow(color: .black, offset: .zero, radius: 5.0, opacity: 0.2)
    hud.textLabel.text = message
    hud.show(in: windowRootViewController!.view)
    return hud

}

// MARK: - 停止所有加载动画
public func hideHUD() {
    JGProgressHUD.allProgressHUDs(in: windowRootViewController!.view).forEach { $0.dismiss() }
}


// MARK: - 成功提示动画
public func showSuccessHUD(message: String?, detail: String?) {
    let hud = JGProgressHUD.init(style: .dark)
    hud.vibrancyEnabled = true
    hud.indicatorView = JGProgressHUDSuccessIndicatorView()
    hud.shadow = JGProgressHUDShadow(color: .black, offset: .zero, radius: 5.0, opacity: 0.2)
    hud.textLabel.text = message
    hud.detailTextLabel.text = detail
    hud.show(in: windowRootViewController!.view)
    hud.dismiss(afterDelay: (message.or("") + detail.or("")).smartDelaySecondsForToastText, animated: true)
}

// MARK: - 错误提示动画
public func showErrorHUD(message: String?, detail: String?) {
    let hud = JGProgressHUD.init(style: .dark)
    hud.vibrancyEnabled = true
    hud.indicatorView = JGProgressHUDErrorIndicatorView()
    hud.shadow = JGProgressHUDShadow(color: .black, offset: .zero, radius: 5.0, opacity: 0.2)
    hud.textLabel.text = message
    hud.detailTextLabel.text = detail
    hud.show(in: windowRootViewController!.view)
    hud.dismiss(afterDelay: (message.or("") + detail.or("")).smartDelaySecondsForToastText, animated: true)
}


// MARK: - 根据 Storyboard 和 StoryboardID 获取 UIviewController
public func initViewController(_ stroyNamed: String, controllerID: String) -> UIViewController {
    let story = UIStoryboard(name: stroyNamed, bundle: nil)
    let vc = story.instantiateViewController(withIdentifier: controllerID)
    return vc
}

//设置 UILabel的行间距
public func labelLineSpace(attributStr: NSAttributedString?, str: String, height: CGFloat) -> NSAttributedString {

    var attribut: NSMutableAttributedString
    if attributStr != nil {
        attribut = NSMutableAttributedString(attributedString: attributStr!)
    } else {
        attribut = NSMutableAttributedString(string: str)
    }

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = height

    attribut.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: NSRange.init(location: 0, length: str.count))
    return attribut
}

// MARK: 添加1px的线，需要自己添加约束或frame
public func addLineView(view: UIView, color: UIColor) -> UIView {
    let v = UIView()
    v.backgroundColor = color
    view.addSubview(v)
    return v
}

public func makeToast(message: String) {
    applicationKeyWindow?.makeToast(message, duration: message.smartDelaySecondsForToastText, position: CSToastManager.defaultPosition())
}

extension Kingfisher where Base: ImageView {

    /// - 扩展 Kingfisher 设置头像图片

    func setHeadImage(_ urlString: String?) {
        self.setImage(with: urlString?.url, placeholder: placeholderHeadImage)
    }

    func setImage(_ urlString: String?) {
        self.setImage(with: urlString?.url, placeholder: placeholderImage)
    }

}

/**
 *  清除Kingfisher的缓存
 */
public func clearKingfisherCache() {
    let kfCache = KingfisherManager.shared.cache
    kfCache.clearDiskCache()
    kfCache.clearMemoryCache()
    kfCache.cleanExpiredDiskCache()
}
/**
 *  字节转换
 */
public func byteSizeConversion(bSize: Float) -> String {
    var size: String
    if bSize < 1024 {
        size = String(format: "%.0f字节", bSize)
    } else if(bSize < 1024 * 1024) {
        size = String(format: "%.2fK", bSize / 1024.0)
    } else if(bSize < 1024 * 1024 * 1024) {
        size = String(format: "%.2fM", bSize / 1024.0 / 1024.0)
    } else {
        size = String(format: "%.2fG", bSize / 1024.0 / 1024.0 / 1024.0)
    }
    return size
}

public func prepareQRCodeView(content:String,size: CGSize,qrColor:UIColor = .black,backgroundColor: UIColor = .white)-> UIImage {

    return LBXScanWrapper.createCode(codeType: "CIQRCodeGenerator", codeString: content, size: size, qrColor: qrColor, bkColor: backgroundColor)!
}
